package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.SessionThreadLocal;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 提交订单 并将所有数据存入数据库中
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R save(@RequestBody Orders orders){

        boolean flag = ordersService.submit(orders);
        if (!flag){
            return R.error("下单失败");
        }
        return R.success();
    }

    /**
     * 客户端查询 所有订单以及订单明细
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(@RequestParam(required = false,defaultValue = "1") Integer page,
                                       @RequestParam(required = false,defaultValue = "10") Integer pageSize  ){

        Page<OrdersDto> ordersDtoPage = new Page<>();

        //查询订单数据
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> ordersWrapper = new LambdaQueryWrapper<>();
        ordersWrapper.eq(Orders::getUserId,SessionThreadLocal.getThreadLocal());
        ordersWrapper.orderByDesc(Orders::getCheckoutTime);

        ordersService.page(ordersPage, ordersWrapper);
        List<OrdersDto> ordersDtos = ordersPage.getRecords().stream().map(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            //将订单信息存入dto
            BeanUtils.copyProperties(orders, ordersDto);
            //查询订单明细列表
            LambdaQueryWrapper<OrderDetail> orderDetailWrapper = new LambdaQueryWrapper<>();
            orderDetailWrapper.eq(OrderDetail::getOrderId,ordersDto.getNumber());
            List<OrderDetail> orderDetails = orderDetailService.list(orderDetailWrapper);
            //将订单明细列表存入dto
            ordersDto.setOrderDetails(orderDetails);
            return ordersDto;
        }).collect(Collectors.toList());

        //将封装好的dto存入分页信息
        ordersDtoPage.setRecords(ordersDtos);
        ordersDtoPage.setTotal(ordersPage.getTotal());

        return R.success(ordersDtoPage);
    }

    /**
     * 订单分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page<Orders>> page(@RequestParam(required = false, defaultValue = "1" ) Integer page,
                  @RequestParam(required = false, defaultValue = "10" ) Integer pageSize,
                                Long number,
                  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
       log.info("订单查询开始了....");

        Page<Orders> ordersPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> ordersWrapper = new LambdaQueryWrapper<>();
        //判断订单号
        ordersWrapper.like(number != null,Orders::getNumber,number);

        //判断携带日期
        ordersWrapper.between(beginTime != null,Orders::getOrderTime,beginTime,endTime);

        ordersService.page(ordersPage,ordersWrapper);

        return R.success(ordersPage);
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R updateStatus(@RequestBody Orders orders){
        ordersService.updateById(orders);

        return R.success();
    }

    /**
     * 再来一单
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R again(@RequestBody Orders orders){

        ordersService.saveAgain(orders);
        return R.success();
    }
}
